package com.yingxin.yxerp.module.report.task.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskName;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yxerp.module.report.task.common.util.QuartzManager;
import com.yingxin.yxerp.module.report.task.entity.DynamicTask;
import com.yingxin.yxerp.module.report.task.mapper.DynamicTaskMapper;
import com.yingxin.yxerp.module.report.task.service.IDynamicTaskService;
import com.yingxin.yxerp.module.report.task.vo.req.DynamicTaskQueryVo;
import com.yingxin.yxerp.module.report.task.vo.req.DynamicTaskUpdateVo;
import com.yingxin.yxerp.module.report.task.vo.resp.DynamicTaskRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务 服务类 实现类
 * @author yuebao
 * @date 2023-11-25
 */

@Service
public class DynamicTaskServiceImpl
        extends ServiceImpl<DynamicTaskMapper, DynamicTask>
        implements IDynamicTaskService {

    @Autowired
    private QuartzManager quartzManager;

    /**
     * 获取 mapper
     */
    @Override
    public DynamicTaskMapper mapper(){
        return baseMapper;
    }

    /**
     * 查询全部
     */
    @Override
    public List<DynamicTaskRespVo> listVo(DynamicTaskQueryVo queryVo ){
        queryVo.setTaskNames( DynamicTaskName.getLikeTypeNames( queryVo.getTaskName() ) );
        List<DynamicTaskRespVo> list = mapper().listVo( queryVo );
        for( DynamicTaskRespVo vo : list ){
            vo.setIsExists( quartzManager.myCheckExists( vo.getId() ) );
        }
        return list;
    }

    /**
     * 任务修改
     */
    @Override
    public void update( DynamicTaskUpdateVo updateVo ){
        if ( StringUtils.isEmpty( updateVo.getTaskName() ) ) {
            throw new BizException("任务名称不能为空！");
        }
        if ( StringUtils.isEmpty( updateVo.getTaskType() ) ) {
            throw new BizException("任务分类不能为空！");
        }
        if ( StringUtils.isEmpty( updateVo.getCron() ) ) {
            throw new BizException("请输入cron表达式！");
        }

        DynamicTask dynamicTask = getById( updateVo.getId() );
        dynamicTask.setTaskType( updateVo.getTaskType() );
        dynamicTask.setTaskName( updateVo.getTaskName() );
        dynamicTask.setCron( updateVo.getCron() );
        dynamicTask.setStatus( updateVo.getStatus() );
        dynamicTask.setRemark( updateVo.getRemark() );
        dynamicTask.setSort( updateVo.getSort() );
        dynamicTask.setUpdaterId(updateVo.getUpdaterId() );

        updateById( dynamicTask );

        // 修改定时任务
        quartzManager.update(
                dynamicTask.getId(),
                dynamicTask.getCron(),
                JSON.toJSONString( dynamicTask.getParam() ),
                dynamicTask.getStatus(),
                dynamicTask.getClassName()
        );
    }

    /**
     * 任务执行一次
     */
    @Override
    public void run( String id ) {
        if ( StringUtils.isEmpty( id ) ) {
            throw new BizException("未找到该任务！");
        }
        DynamicTask dynamicTask = getById( id );
        if ( ObjectUtil.isEmpty( dynamicTask ) ) {
            throw new BizException("未找到该任务！");
        }
        quartzManager.run( id );
    }

    /**
     * 任务启动/停止
     */
    @Override
    public void updateStatus( DynamicTask dynamicTask ) {
        if ( ObjectUtil.isEmpty( dynamicTask ) ) {
            throw new BizException("未找到该任务！");
        }
        if ( StringUtils.isEmpty( dynamicTask.getId() ) ) {
            throw new BizException("未找到该任务！");
        }
        DynamicTask backupTask_check = getById( dynamicTask.getId() );
        if ( ObjectUtil.isEmpty( backupTask_check ) ) {
            throw new BizException("未找到该任务！");
        }
        backupTask_check.setStatus( dynamicTask.getStatus() );
        updateById( backupTask_check );
        // 启动
        if ( backupTask_check.getStatus() ) {
            quartzManager.start( backupTask_check.getId() );
        }
        // 停止
        else {
            quartzManager.stop( backupTask_check.getId() );
        }
    }

    /**
     * 任务删除
     */
    @Override
    public void delete( String id ) {
        if ( StringUtils.isEmpty( id ) ) {
            throw new BizException("未找到该任务！");
        }
        DynamicTask backupTask = getById( id );
        if ( ObjectUtil.isEmpty( backupTask ) ) {
            throw new BizException("未找到该任务！");
        }
        if ( backupTask.getStatus() ) {
            throw new BizException ("请先停止任务，再删除");
        }

        // 删除
        removeById( id );

        // 删除定时任务
        quartzManager.delete( id );
    }

    /**
     * 联机
     */
    @Override
    public void online(String id){
        DynamicTask dynamicTask = getById( id );
        // 修改定时任务
        quartzManager.add(
                dynamicTask.getId(),
                dynamicTask.getCron(),
                JSON.toJSONString( dynamicTask.getParam() ),
                dynamicTask.getStatus(),
                dynamicTask.getClassName()
        );
    }

}
